/*
 * NCSSMetrics.java
 * 
 * Copyright (c) 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dcarew.javancss.metrics;

import java.text.NumberFormat;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

/**
 * 
 * 
 * @author Devon Carew
 */
public class NCSSMetrics
{
	private static NumberFormat	nf	= NumberFormat.getNumberInstance();
	private static NumberFormat	df	= NumberFormat.getNumberInstance();
	
	static
	{
		df.setMaximumFractionDigits(2);
		df.setMinimumFractionDigits(2);
	}
	
	
	private int		fileCount;
	private Set		packageSet = new HashSet();
	private long	byteCount;
	
	/** NCSS */
	private int		ncssCount;
	
	/** Lines of comments? */
	private int		locCount;
	
	private int		javaDocCount;
	
	private double	funcNCSS;
	private double	funcCycByNCSS;
	
	
	public NCSSMetrics()
	{

	}

	// file count
	public void incFileCount()
	{
		setFileCount(getFileCount() + 1);
	}

	public int getFileCount()
	{
		return fileCount;
	}
	
	public void setFileCount(int fileCount)
	{
		this.fileCount = fileCount;
	}
	
	public String getFileCountText()
	{
		return nf.format(getFileCount());
	}
	
	// byte count
	public void incByteCount(long increment)
	{
		setByteCount(getByteCount() + increment);
	}

	public long getByteCount()
	{
		return byteCount;
	}

	public void setByteCount(long byteCount)
	{
		this.byteCount = byteCount;
	}

	public String getByteCountText()
	{
		return nf.format(((getByteCount() + 1023) / 1024)) + "k";
	}
	
	// ncss count
	public void incNCSSCount(int increment)
	{
		setNcssCount(getNcssCount() + increment);
	}

	public int getNcssCount()
	{
		return ncssCount;
	}
	
	public void setNcssCount(int ncssCount)
	{
		this.ncssCount = ncssCount;
	}
	
	public String getNcssCountText()
	{
		return nf.format(getNcssCount());
	}
	
	// loc count
	public void incLOCCount(int increment)
	{
		setLOCCount(getLOCCount() + increment);
	}
	
	public int getLOCCount()
	{
		return locCount;
	}

	public void setLOCCount(int locCount)
	{
		this.locCount = locCount;
	}
	
	// package count
	public void incPackageCount(Hashtable packages)
	{
		for (Enumeration keys = packages.keys(); keys.hasMoreElements(); )
			packageSet.add(keys.nextElement());
	}
	
	public int getPackageCount()
	{
		return packageSet.size();
	}
	
	public String getPackageCountText()
	{
		return nf.format(getPackageCount());
	}
	
	// javadoc count
	public void incJavaDocCount(int increment)
	{
		setJavaDocCount(getJavaDocCount() + increment);
	}
	
	public int getJavaDocCount()
	{
		return javaDocCount;
	}

	public void setJavaDocCount(int javaDocCount)
	{
		this.javaDocCount = javaDocCount;
	}
	
	public String getJavaDocText()
	{
		return nf.format(getJavaDocCount());
	}
	
	// cyc count
	public void incCycCount(Vector functions)
	{
		for (int i = 0; i < functions.size(); i++)
		{
			Vector function = (Vector)functions.get(i);
			
			int funcNCSS = ((Integer)function.get(1)).intValue();
			int funcCYC = ((Integer)function.get(2)).intValue();
			
			this.funcNCSS += funcNCSS;
			this.funcCycByNCSS += (funcNCSS * funcCYC);
		}
	}
	
	public double getAverageCycCount()
	{
		if (funcNCSS == 0.0)
			return 0.0;
		
		return funcCycByNCSS / funcNCSS;
	}
	
	public String getAverageCycCountText()
	{
		return df.format(getAverageCycCount());
	}
	
	public String toString()
	{
		String ret = "";
		
		ret += "NCSS: " + nf.format(getNcssCount()) + "\n";
		ret += "File count: " + nf.format(getFileCount()) + "\n";
		ret += "Total Bytes: " + nf.format(((getByteCount() + 1023) / 1024)) + "k\n";
		
		return ret;
	}

}
